//
// Created by ECNU on 2024/2/22.
//

#include <nanofaiss/IndexFlatCodes.h>
#include <nanofaiss/impl/FaissAssert.h>

namespace faiss {

IndexFlatCodes::IndexFlatCodes(): code_size(0) {}

IndexFlatCodes::IndexFlatCodes(size_t code_size, idx_t d, MetricType metric)
        : Index(d, metric), code_size(code_size)  {}

void IndexFlatCodes::add(idx_t n, const float* x) {
    FAISS_THROW_IF_NOT(is_trained); // index must be trained(or not need trained) before add
    if (n == 0) return;
    codes.resize((ntotal + n) * code_size);
    sa_encode(n, x, codes.data() + (ntotal * code_size)); // must be implemented by subclass
    ntotal += n;
}

size_t IndexFlatCodes::sa_code_size() const {
    return code_size;
}

void IndexFlatCodes::reset() {
    codes.clear();
    ntotal = 0;
}

}
